<?php
$action=fs_get_value("ctr");
if($_SESSION['sid'] == '')
{
    fs_showmsg("请先登录","?do=do");
    exit();
}
switch($action)
{
	case    "test"  :   test()  ;   break;
	case	"point"	:	point()	;	break;
	case    "calculate_point" : get_result() ; break;
	default			:	main()	;	break;
}
?>
<?php
//试题MAIN
function main(){
	global $db;
	$test_id = intval(fs_get_value("id"));
?>

<div class="splist_content">
    <div class="splist_content_left_test">
		<div class="splist_content_left_content_test">
		    <div class="test">
				<!--testhead-->
				<div class="test_head">
					<div class="exercise_head_left"></div>
					<div class="test_head_center"></div>
					<div class="test_head_right"></div>
				</div>
				
				<div class="test_title">
					<?php echo(get_test_info($test_id,'exam_name'));?>
				</div>
				<div class="test_info">
					已经有 <font color="#FF9933"><?php echo(get_test_info($test_id,'exam_num'));?></font> 人做过此试卷
				</div>
			
				<!--题目列表开始-->
				<div class="test_body">
				 <form action="?do=exerciseinfo&ctr=calculate_point&test_id=<?php echo($test_id);?>" method="post" >
					<?php
						$sql = "select * from ".DBPREFIX."exam_thread where exam_id=".$test_id." order by thread_sort ASC";
						$result=$db->query($sql);
						$i=0;
						while($row= $db->fetch_array($result)){
                            switch($row['thread_type'])
                            {
                                case 1:$row['type']='单项选择题';
                                    break;
                                case 2:$row['type']='多项选择题';
                                    break;
                                case 3:$row['type']='判断题';
                                    break;
                                default:$row['type']='';
                            }
		?> 		
					<div class="test_body_part">
						<B><img src="resources/images/heading2-bullet.png" height="10" /> <?php echo $row['type'] ?>(<?php echo $row['thread_des'] ?>)</B>
					</div>
							<?php
								$result_question =$db->query("select a.*,b.* from ".DBPREFIX."examconnect as a inner join ".DBPREFIX."question as b on b.q_id=a.q_id where a.thread_id=".$row['thread_id']." order by b.q_sort desc");
								//echo "select a.*,b.* from ".DBPREFIX."examconnect as a inner join ".DBPREFIX."question as b on b.q_id=a.q_id where a.thread_id=".$row['thread_id']." order by b.q_sort desc";
                                $num = $db -> num_rows($result_question);
								while($row_question = $db -> fetch_array($result_question)){
                                    $options = explode('|',$row_question['q_option']);
                                    $option_str='';
                                    foreach($options as $key=>$op)
                                    {
                                        $delete_p_op = str_replace('<p>','',$op);
                                        $delete_p_op = str_replace('</p>','',$delete_p_op);
                                        $option_s = array('A','B','C','D','E','F','G','H','I','G');
                                        $option_str .=$option_s[$key].":".$delete_p_op."<br/>";
                                    }
									$i++;
                                    $row_question['q_title'] =str_replace('<p>','',$row_question['q_title']);
                                    $row_question['q_title'] =str_replace('</p>','',$row_question['q_title']);//给标题二次删除p
					?>
				   
					<!--item-->		
					<div class="test_body_item">
						<div class="test_body_item_question">
							   第<font style="color:#FF0000;"><?php echo($i);?></font>题&nbsp&nbsp<?php echo $row_question['q_title']?>
						</div>
						<div class="test_body_item_answer">
							  <?php echo $option_str ?>
						</div>
					
						<div class="test_body_item_checkbox" style="line-height:21px;">
							请选择答案： <?php
											  switch($row_question['q_type']){//
												  case 1://1为单选?>
												  <input type="radio" name="answer_radio<?php echo($row_question['q_id']);?>" value="A" /> A 
												  <input type="radio" name="answer_radio<?php echo($row_question['q_id']);?>" value="B" /> B  
												  <input type="radio" name="answer_radio<?php echo($row_question['q_id']);?>" value="C" /> C 
												  <input type="radio" name="answer_radio<?php echo($row_question['q_id']);?>" value="D" /> D 
												  <?php ; break;
												  case 2://2为多选?>
												  <input type="checkbox" name="more_options<?php echo($row_question['q_id']);?>[]" value="A" /> A 
												  <input type="checkbox" name="more_options<?php echo($row_question['q_id']);?>[]" value="B" /> B 
												  <input type="checkbox" name="more_options<?php echo($row_question['q_id']);?>[]" value="C" /> C 
												  <input type="checkbox" name="more_options<?php echo($row_question['q_id']);?>[]" value="D" /> D
												  <?php ; break;
												  case 3://3为判断?>
												  <input type="radio" name="answer_judge<?php echo($row_question['q_id']);?>" value="A" /> A 
												  <input type="radio" name="answer_judge<?php echo($row_question['q_id']);?>"value="B" /> B 
												  <?php ; break;
			}
			?>
						</div>
					</div>
					<!--end-->
					<?php
						}
					  }
					?>
					<div style="width:55%;text-align:right;padding:10px; float:left;"><input type="submit" value="提交答案" /> <input type="button" value="返回上一页" onclick="javascript:history.back(-1)" /></div>
		</form>
				</div>
				<!--end-->
			</div>
			</div>
		</div>
	</div>
<?php
}	

//显示解析
function get_result(){
	global $db;
	$test_id = intval(fs_get_value('test_id'));
	$sql = "update ".DBPREFIX."exam set exam_num = exam_num + 1 where exam_id = ".$test_id;//做完试卷 试卷+1
	$db -> query($sql);
    $insert_sql = "insert into ".DBPREFIX."record (`uid`,`cid`,`datetime`,`type`) values(".$_SESSION['sid'].",".$test_id.",".time().",3)";//添加试题记录
    $db->query ($insert_sql);
?>
<div class="splist_content">
    <div class="splist_content_left_test">
		<div class="splist_content_left_content_test">
		    <div class="test">
				<!--testhead-->
				<div class="test_head">
					<div class="exercise_head_left"></div>
					<div class="test_head_center"></div>
					<div class="test_head_right"></div>
				</div>
				
				<div class="test_title">
					<?php echo(get_test_info($test_id,'exam_name'));?>
				</div>
				<div class="test_info">
					考试时间：<?php echo(get_test_info($test_id,'exam_time'));?>&nbsp;&nbsp;&nbsp;&nbsp;已经有 <font color="#FF9933"><?php echo(get_test_info($test_id,'exam_num'));?></font> 人做过此试卷&nbsp;&nbsp;&nbsp;&nbsp;试卷总分:<font color="#FF9933"><?php echo(get_test_info($test_id,'exam_score'));?></font>
				</div>
			
				<!--题目列表开始-->
				<div class="test_body">
				 <form action="test/?do=calculate_point&test_id=<?php echo($test_id);?>" method="post">
					<?php
						$sql = "select * from ".DBPREFIX."exam_thread where exam_id=".$test_id." order by thread_sort ASC";
						$result=$db->query($sql);
						$i=0;
						while($row= $db->fetch_array($result)){
                            switch($row['thread_type'])
                            {
                                case 1:$row['type']='单项选择题';
                                    break;
                                case 2:$row['type']='多项选择题';
                                    break;
                                case 3:$row['type']='判断题';
                                    break;
                                default:$row['type']='';
                            }
		?> 		
					<div class="test_body_part">
						<B><img src="resources/images/heading2-bullet.png" height="10" /> <?php echo $row['type'] ?>(<?php echo $row['thread_des'] ?>)</B>
					</div>
							<?php
								$result_question =$db->query("select a.*,b.* from ".DBPREFIX."examconnect as a inner join ".DBPREFIX."question as b on b.q_id=a.q_id where a.thread_id=".$row['thread_id']." order by b.q_sort asc");
								//echo "select a.*,b.* from ".DBPREFIX."examconnect as a inner join ".DBPREFIX."question as b on b.q_id=a.q_id where a.thread_id=".$row['thread_id']." order by b.q_sort desc";
                                $num = $db -> num_rows($result_question);
								while($row_question = $db -> fetch_array($result_question)){
                                    $options = explode('|',$row_question['q_option']);
                                    $option_str='';
                                    foreach($options as $key=>$op)
                                    {
                                        $delete_p_op = str_replace('<p>','',$op);
                                        $delete_p_op = str_replace('</p>','',$delete_p_op);
                                        $option_s = array('A','B','C','D','E','F','G','H','I','G');
                                        $option_str .=$option_s[$key].":". $delete_p_op."<br/>";
                                    }
									$i++;
                                    $row_question['q_title'] =str_replace('<p>','',$row_question['q_title']);
                                    $row_question['q_title'] =str_replace('</p>','',$row_question['q_title']);//给标题二次删除p
					?>
				   
					<!--item-->		
					<div class="test_body_item">
						<div class="test_body_item_question">
							   第<font style="color:#FF0000;"><?php echo($i);?></font>题&nbsp&nbsp<?php echo $row_question['q_title']?>
						</div>
						<div class="test_body_item_answer">
							  <?php echo $option_str ?>
						</div>
                        <?php 
                                        $test_question_id = $row_question['q_id'];
		                                $answer_radio = $_POST['answer_radio'.$test_question_id];
										$more_options = $_POST['more_options'.$test_question_id];
										$answer_judge = $_POST['answer_judge'.$test_question_id];
										$answer_uncertain = $_POST['answer_uncertain'.$test_question_id];?>
				<div class="test_body_item_checkbox" style="line-height:21px; <?php 
										switch($row_question['q_type']){
										case 1:if($answer_radio != str_replace(' ','',$row_question['q_answer'])){echo("border:#FF0000 2px solid;");}else{echo("border:#00FF00 1px dashed;");} ;break;
										case 2:if(count($more_options) == 0 || implode($more_options) != str_replace(',','',$row_question['q_answer'])){echo("border:#FF0000 1px dashed;");}else{echo("border:#00FF00 1px dashed;");} ;break;
										case 3;if($answer_judge != str_replace(' ','',$row_question['q_answer'])){echo("border:#FF0000 2px solid;");}else{echo("border:#00FF00 1px dashed;");} ;break;
										case 4;if(count($answer_uncertain) == 0 || implode($answer_uncertain) != str_replace(' ','',$row_question['q_answer'])){echo("border:#FF0000 2px solid;");}else{echo("border:#00FF00 1px dashed;");} ;break;
										}	
										?>">
                   您选择的答案是：<span style="color:#FF0000;">	
									<?php
		                                switch($row_question['q_type']){
		                                    case 1:if($answer_radio == ''){echo("无");}else{echo $answer_radio;} ;break;//0 是单选题
		                                    case 2:if(count($more_options) == 0){echo("无");}else{ echo $more_options_str=implode(',',$more_options);} ;break;
		                                    case 3:if($answer_judge == ''){echo("无");}else{echo $answer_judge;} ;break;
		                                   // case 3:if(count($answer_uncertain) == 0){echo("无");}else{foreach($answer_uncertain as $value){echo($value);}} ;break;
	                                    }
	                                ?></span>&nbsp;&nbsp;&nbsp;&nbsp;
	                正确答案：<span style="color:#FF0000;"><?php echo($row_question['q_answer']);?></span></br>
					答案解析：<?php echo($row_question['q_jiexi']);?>
				</div>
			</div>
			<!--end-->
			<?php
				}
			  }
			?>
           <div style="width:45%;text-align:right;padding:10px; float:left;"><input type="button" value="返回上一页" onclick="javascript:history.go(-2)" /></div>
		</div>
		<!--end-->
				</div>
			</div>
		</div>
	</div>
<?php
}